﻿using System.Collections.Specialized;
using System.Configuration;
using System.ServiceModel;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using M2B.GestionEntretiens.Contracts;
using M2B.GestionEntretiens.DAL;
using M2B.GestionEntretiens.Models;

namespace M2B.GestionEntretiens
{
    // Remarque : pour obtenir des instructions sur l'activation du mode classique IIS6 ou IIS7, 
    // visitez http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private IWindsorContainer _container;
        protected void Application_Start()
        {
            

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

        }
        protected void Session_Start(object sender, EventArgs e)
        {
            //Il faut définit la chaine de connexion à deux emplacements différents 
            //1) au niveau du webConfig du projet Web (M2B.GestionEntretiens)
            //2) au nivau du webConfig du projetHost des services WCF qui hébergera les services (M2B.GestionEntretiens.Host)
            var connectionString = ConfigurationManager.ConnectionStrings["GestionEntretiens"].ConnectionString;
            var section = (ConfigWCFServicesHelper)ConfigurationManager.GetSection("WCFServices");
            if (section != null)
            {
                

                _container = new WindsorContainer();
                _container.AddFacility<WcfFacility>().Register
                (
                  Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>().DependsOn(Property.ForKey("connectionString").Eq(connectionString)),
                  Component.For<ICandidatsService>()
                  .AsWcfClient(new DefaultClientModel
                  {
                      Endpoint = WcfEndpoint.BoundTo(new BasicHttpBinding() { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 })
                                            .At(new Uri((section.HashKeys["CandidatsService"] as WCFServicesElement).Value))
                  }),
                  Component.For<IReferentielsService>()
                  .AsWcfClient(new DefaultClientModel
                  {

                      Endpoint = WcfEndpoint.BoundTo(new BasicHttpBinding() { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 })
                                            .At(new Uri((section.HashKeys["ReferentielsService"] as WCFServicesElement).Value))
                  }),
                   Component.For<ITestsService>()
                  .AsWcfClient(new DefaultClientModel
                  {
                      Endpoint = WcfEndpoint.BoundTo(new BasicHttpBinding() { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 })
                                            .At(new Uri((section.HashKeys["TestsService"] as WCFServicesElement).Value))
                  }),
                 Component.For<IQuestionsService>()
                  .AsWcfClient(new DefaultClientModel
                  {
                      Endpoint = WcfEndpoint.BoundTo(new BasicHttpBinding(){MaxReceivedMessageSize = 2147483647,MaxBufferSize = 2147483647})
                                            .At(new Uri((section.HashKeys["QuestionsService"] as WCFServicesElement).Value))
                  }),
                 Component.For<IEntretiensService>()
                  .AsWcfClient(new DefaultClientModel
                  {
                      Endpoint = WcfEndpoint.BoundTo(new BasicHttpBinding(){MaxReceivedMessageSize = 2147483647,MaxBufferSize = 2147483647})
                                            .At(new Uri((section.HashKeys["EntretiensService"] as WCFServicesElement).Value))
                  }),
                   Component.For<IExamensService>()
                  .AsWcfClient(new DefaultClientModel
                  {
                      Endpoint = WcfEndpoint.BoundTo(new BasicHttpBinding(){MaxReceivedMessageSize = 2147483647,MaxBufferSize = 2147483647})
                                            .At(new Uri((section.HashKeys["ExamensService"] as WCFServicesElement).Value))
                  })
                );

                
                Session["Container"] = _container;
                //Chargement du réferentiel de l'application
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel == null && _container!=null)
                {
                    refrentielsModel = new RefrentielsModel(_container.Resolve<IReferentielsService>());
                    Session["RefrentielsModel"] = refrentielsModel;
                }
            }




        }

    }
}